
export default{
	//设置Cookie
	setCookie: function(name,value,time){
		var strsec = this.getsec(time);
		var exp = new Date();
		exp.setTime(exp.getTime() + strsec*1);
		document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
	},
	//设置Cookie过期时间
	getsec: function(str){
		var str1=str.substring(1,str.length)*1;
		var str2=str.substring(0,1);
		if (str2=="s"){
			return str1*1000;
		}else if (str2=="h"){
			return str1*60*60*1000;
		}else if (str2=="d"){
			return str1*24*60*60*1000;
		}
	},
	//读取Cookie
	getCookie: function(name){
		var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
		if(arr=document.cookie.match(reg)){
			return unescape(arr[2]);
		}else{
			return null;
		}
	},
	//删除Cookie
	delCookie: function(name){
		var exp = new Date();
		exp.setTime(exp.getTime() - 1);
		var cval = this.getCookie(name);
		if(cval != null){
			document.cookie= name + "="+cval+";expires="+exp.toGMTString();
		}
	},
	// 格式化时间
	goodTime (str) {
		let now = new Date().getTime()
		let oldTime = new Date(str).getTime()
		let difference = now - oldTime
		let result = ''
		let minute = 1000 * 60
		let hour = minute * 60
		let day = hour * 24
		let month = day * 30
		let year = month * 12
		let _year = difference / year
		let _month = difference / month
		let _week = difference / (7 * day)
		let _day = difference / day
		let _hour = difference / hour
		let _min = difference / minute

		if (_year >= 1) {
			result = '发表于 ' + ~~(_year) + ' 年前'
		} else if (_month >= 1) {
			result = '发表于 ' + ~~(_month) + ' 个月前'
		} else if (_week >= 1) {
			result = '发表于 ' + ~~(_week) + ' 周前'
		} else if (_day >= 1) {
			result = '发表于 ' + ~~(_day) + ' 天前'
		} else if (_hour >= 1) {
			result = '发表于 ' + ~~(_hour) + ' 个小时前'
		} else if (_min >= 1) {
			result = '发表于 ' + ~~(_min) + ' 分钟前'
		} else {
			result = '刚刚'
		}
		return result
	},
	// 距离0时还有多少时间
	endingToday(){
		let _cur_time = new Date().getTime();
		
		let dateValue = new Date(new Date(new Date(_cur_time).toLocaleDateString()).getTime()+24*60*60*1000-1);
		let _time = dateValue.getFullYear() + '-' + ((dateValue.getMonth() + 1) < 10 ? "0" + (dateValue.getMonth() + 1):(dateValue.getMonth() + 1))+ "-" + (dateValue.getDate() < 10 ? "0" + dateValue.getDate():dateValue.getDate()) + ' ' + dateValue.getHours() + ':' + dateValue.getMinutes() + ':' + dateValue.getSeconds();

		let _end_time = new Date(_time).getTime(); 	 // 结束时间

		let difference = _end_time - _cur_time	// 时间距离
		let result = ''
		let second = 1000
		
		let _sec = difference / second

		result = `s${~~(_sec)}`

		return result
	},
	// 验证
	isNull (str) {
		return (str == "" || typeof str != "string");
	},
	betweenLength (str, _min, _max) {
		return (str.length >= _min && str.length <= _max);
	},
	isUid (str) {
		return new RegExp(authRegExp.username).test(str);
	},
	fullNumberName (str) {
		return new RegExp(authRegExp.fullNumber).test(str);
	},
	floatNum (str) {
		return new RegExp(authRegExp.decimal).test(str);
	},
	isPwd (str) {
		return /^.*(?=.{6,})(?=.*\d)(?=.*[a-zA-Z]).*$/i.test(str);
	},
	isPwdRepeat (str1, str2) {
		return (str1 == str2);
	},
	isEmail (str) {
		return new RegExp(authRegExp.email).test(str);
	},
	isTel (str) {
		return new RegExp(authRegExp.tel).test(str);
	},
	isLetter (str) {
		return new RegExp(authRegExp.letter).test(str);
	},
	isChinese (str) {
		return new RegExp(authRegExp.chinese).test(str);
	},
	isMobile (str) {
		return new RegExp(authRegExp.mobile).test(str);
	},
	isIdcard: function (str) {
		return new RegExp(authRegExp.idcard).test(str);
	},
	checkType (element) {
		return (element.attr("type") == "checkbox" || element.attr("type") == "radio" || element.attr("rel") == "select");
	},
	isRealName (str) {
		return new RegExp(authRegExp.realname).test(str);
	},
	isMoney (str) {
		return new RegExp(authRegExp.money).test(str);
	},

}

var authRegExp = {
	integer: "^-?[1-9]\\d*$", //整数
	integer1: "^[1-9]\\d*$", //正整数
	integer2: "^-[1-9]\\d*$", //负整数
	number: "^([+-]?)\\d*\\.?\\d+$", //数字
	number1: "^[1-9]\\d*|0$", //正数（正整数 + 0）
	number2: "^-[1-9]\\d*|0$", //负数（负整数 + 0）
	decimal: "^([+-]?)\\d*\\.\\d+$", //浮点数
	decimal1: "^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*$", //正浮点数
	decimal2: "^-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*)$", //负浮点数
	decimal3: "^-?([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0)$", //浮点数
	decimal4: "^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0$", //非负浮点数（正浮点数 + 0）
	decimal5: "^(-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*))|0?.0+|0$", //非正浮点数（负浮点数 + 0）
	ascii: "^[\\x00-\\xFF]+$", //仅ACSII字符
	chinese: "^[\\u4e00-\\u9fa5]+$", //仅中文
	color: "^[a-fA-F0-9]{6}$", //颜色
	date: "^\\d{4}(\\-|\\/|\.)\\d{1,2}\\1\\d{1,2}$", //日期
	email: "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$", //邮件
	idcard: "^[1-9]([0-9X]{14}|[0-9X]{17})$", //身份证
	ip4: "^(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)$", //ip地址
	letter: "^[A-Za-z]+$", //字母
	letterL: "^[a-z]+$", //小写字母
	letterU: "^[A-Z]+$", //大写字母
	mobile: "^((13[0-9])|(15[^4,\\D])|(16[0-9])|(17[0-9])|(14[0-9])|(19[0-9])|(18[0-9]))\\d{8}$", //手机
	password: "^.*[A-Za-z0-9\\w_-]+.*$", //密码
	fullNumber: "^[0-9]+$", //数字
	picture: "(.*)\\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$", //图片
	qq: "^[1-9]*[1-9][0-9]*$", //QQ号码
	rar: "(.*)\\.(rar|zip|7zip|tgz)$", //压缩文件
	tel: "^[0-9\-()（）]{7,18}$", //电话号码的函数(包括验证国内区号,国际区号,分机号)
	url: "^http[s]?:\\/\\/([\\w-]+\\.)+[\\w-]+([\\w-./?%&=]*)?$", //url
	username: "^[A-Za-z0-9_\\-\\u4e00-\\u9fa5]+$", //用户名
	realname: "^[A-Za-z\\u4e00-\\u9fa5]+$", // 真实姓名
	zipcode: "^\\d{6}$", //邮编
	notempty: "^\\S+$" //非空
};

// 去首尾空格

if(typeof(String.prototype.trim) === "undefined")
{
    String.prototype.trim = function() 
    {
        return String(this).replace(/^\s+|\s+$/g, '');
    };
}